<template>
	<view class="content">
		<view class="login-bg"></view>
		<view class="Loginform">
			<view class="userName">
				<view class="login-icon">
					<u-image width="23rpx" height="35rpx" src="../../static/login/mobile.png"></u-image>
				</view>
				<view style="flex:1;padding-right: 30rpx;">
					<u-input maxlength="11" v-model="userName" placeholder="请输入您的账号" :password-icon="true"></u-input>
				</view>
			</view>

			<view class="password">
				<view class="login-icon">
					<u-image width="22rpx" height="33rpx" src="../../static/login/lock.png"></u-image>
				</view>
				<view style="flex:1;padding-right: 30rpx;">
					<u-input type="password" v-model="password" placeholder="请输入您的密码"></u-input>
				</view>
			</view>
			<view class="btn">
				<button type="primary" class="loginBtn" @click="login">登 录</button>
			</view>
			<view style="margin-top: 225rpx;">
				<view class="pageButton">
					<text class="line"></text>
					<text style="padding:0 20rpx;">快步科技管理中心</text>
					<text class="line"></text>
				</view>
				<view class="pageButton">&copy 2020-2021 All Rights Reserved</view>
			</view>
		</view>
	</view>
</template>
<script>
	import md5Libs from "uview-ui/libs/function/md5";
	export default {
		data() {
			return {
				userName: "", //13890755825,15528555558
				password: ""
			}
		},
		methods: {
			login() {
				if (!this.userName) {
					uni.showToast({
						title: '账号不能为空！',
						icon: 'none'
					});
					return;
				}
				if (!(/^1(3|4|5|6|7|8|9)\d{9}$/.test(this.userName))) {
					uni.showToast({
						title: '请输入正确的账号！',
						icon: 'none'
					});
					return
				}
				if (!this.password) {
					uni.showToast({
						icon: 'none',
						title: '密码不能为空！'
					});
					return;
				}
				uni.removeStorage('token');
				uni.removeStorage('mid');
				uni.removeStorage('merShortName');
				this.$u.api.login({
					userName: this.userName,
					password: this.password ? md5Libs.md5(this.password) : ''
				}).then(res => {
					if (res.code == 0) {
						uni.setStorageSync('token', res.data.access_token);
						this.getRoles();
					} else {
						uni.showToast({
							icon: 'none',
							title: res.msg
						});
					}
				})
			},
			getRoles() {
				this.$u.api.roles().then(res => {
					if (res.code == 0) {
						if (res.data.length == 2) {
							uni.navigateTo({
								url: "/pages/rols/index"
							})
						} else {			
							if (res.data[0].availableStatus == 0) {
								uni.setStorageSync('mid', JSON.stringify(res.data[0].merchantId));
								uni.setStorageSync('merShortName', JSON.stringify(res.data[0].merchantShortName));
								uni.setStorageSync('type', JSON.stringify(res.data[0].type));//1线上 2线下
								uni.switchTab({
									url: "/pages/rols/home"
								})
							} else {
								uni.showToast({
									icon: 'none',
									title: res.data[0].availableStatusDesc
								});
							}
						}
					} else {
						uni.showToast({
							icon: 'none',
							title: res.msg
						});
					}
				})
			}
		}
	}
</script>

<style scoped>
	.login-bg {
		background-image: url(../../static/login/bg.png);
		background-repeat: no-repeat;
		background-size: cover;
		height: 376rpx;
		width: 100%;
	}

	.Loginform {
		padding: 99rpx 52rpx;

	}

	.userName,
	.password {
		display: flex;
		align-items: center;
		border: 2rpx solid #EBEBEB;
		border-radius: 40rpx;
		margin-bottom: 56rpx;
		height: 86rpx;
	}

	.login-icon {
		width: 50rpx;
		display: flex;
		align-items: center;
		justify-content: flex-end;
		margin-right: 20rpx;
	}

	.btn {
		margin-top: 71rpx;
	}

	.btn button {
		width: 606rpx;
		background-color: #1F6ECF;
		border-radius: 43rpx;
		height: 86rpx;
		line-height: 86rpx;
		font-size: 34rpx;
		box-shadow: 0px 12px 16px 0px rgba(28, 100, 224, 0.32);
	}

	.line {
		background-color: #B3B3B3;
		width: 60rpx;
		height: 2rpx;
		display: inline-block;
	}

	.pageButton {
		display: flex;
		align-items: center;
		justify-content: center;
		color: #999999;
		font-size: 24rpx;
	}

	.loginBtn {
		font-size: 34rpx;
		color: #FFFFFF;
	}
</style>
